package com.kichina.parking.module.message.socket;

import com.google.common.collect.Lists;
import com.kichina.parking.controller.parking.handler.YardStatusInfoHandler;
import com.kichina.parking.module.feign.bean.DeviceLineModel;
import com.kichina.parking.module.feign.deal.CarBanRemoteCall;
import com.kichina.parking.service.logic.bean.YardStatusInfo;
import io.vertx.core.net.NetSocket;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

import java.util.List;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

/**
 * @author : <a href="https://iiiii.run">xiaohan.yuan</a>
 * @version :    1.0
 * @description : 关闭socket处理器
 * @date :    2021-09-15 14:07
 */
@Component
@Scope(value = SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
@Slf4j
public class SocketCloseHandler {

    private final CarBanRemoteCall carBanRemoteCall;
    private final YardStatusInfoHandler yardStatusInfoHandler;

    public SocketCloseHandler(CarBanRemoteCall carBanRemoteCall, YardStatusInfoHandler yardStatusInfoHandler) {
        this.carBanRemoteCall = carBanRemoteCall;
        this.yardStatusInfoHandler = yardStatusInfoHandler;
    }

    public void handler(NetSocket socket) {
        String socketId = socket.writeHandlerID();
        List<DeviceLineModel> deviceLineModels = Lists.newArrayList();
        //关闭中控
        for (String strKey : SocketStorageDeal.socketMap.keySet()) {
            if (strKey.startsWith(socketId)) {
                SocketStorageDeal.removeNetSocket(strKey);
                // 保存信息
                DeviceLineModel deviceLineModel = new DeviceLineModel();
                String code = strKey.substring(socketId.length() + 1, socketId.length() + 11);
                String houseId = strKey.substring(socketId.length() + 12);
                deviceLineModel.setCode(code);
                deviceLineModel.setHouseId(houseId);
                deviceLineModel.setIsOnline(false);
                deviceLineModels.add(deviceLineModel);
                log.info("设备code码为:" + code + "房屋ID为:" + houseId);
            }
        }
        // 向APP云上报设备离线信息
        if (CollectionUtils.isNotEmpty(deviceLineModels)) {
            for (DeviceLineModel deviceLineModel:deviceLineModels) {
                log.info("向APP云服务上报离线状态");
                carBanRemoteCall.cardBanRemotePost(deviceLineModel);
                YardStatusInfo yardStatusInfo = new YardStatusInfo();
                yardStatusInfo.setIsOnline(false);
                yardStatusInfo.setYardKey(deviceLineModel.getCode());
                yardStatusInfo.setYardPassword(deviceLineModel.getHouseId());
                //添加或更新车场在线状态
                yardStatusInfoHandler.saveOrUpdateYardInfo(yardStatusInfo);
            }
        }

        log.info("关闭socket[{}]成功!", socketId);
    }
}
